package il.ac.tau.cvws.scrabblesidekick.client.ui.view.solution;

import il.ac.tau.cvws.scrabblesidekick.client.R;
import il.ac.tau.cvws.scrabblesidekick.client.ui.view.dialog.DialogContent;
import il.ac.tau.cvws.scrabblesidekick.shared.constant.BoardConstants;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/* A view for the solution suggested by the server.
 */
public class SolutionView extends DialogContent
{
	private static final int DIALOG_PADDING	= 20;
	private static final int BOARD_PADDING	= 5;
	
	private final Context _context;
	private final EventHandler _eventHandler;
	private final TileView[][] _tiles;
	
	public SolutionView(Context context, EventHandler eventHandler)
	{
		super(context);
		
		_context = context;
		_eventHandler = eventHandler;
		_tiles = new TileView[BoardConstants.BOARD_TILES_NUM][BoardConstants.BOARD_TILES_NUM];
		
		setOrientation(LinearLayout.HORIZONTAL);
		setGravity(Gravity.CENTER);
		setPadding(DIALOG_PADDING, DIALOG_PADDING, DIALOG_PADDING, DIALOG_PADDING);
		
		LinearLayout mainLayout = new LinearLayout(_context);
		mainLayout.setOrientation(LinearLayout.HORIZONTAL);
		mainLayout.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
		
		mainLayout.addView(buildBoardView());
		mainLayout.addView(buildFacebookView());
		
		addView(mainLayout);
	}
	
	/* Creates a view for the solution 'board'. It is a view simulating the Scrabble
	 * board, with the suggested solution laid out on it.
	 */
	private View buildBoardView()
	{
		LinearLayout boardLayout = new LinearLayout(_context);
		
		ImageView iView  = new ImageView(LayoutInflater.from(_context).getContext());
		iView.setImageDrawable(getResources().getDrawable(R.drawable.ninja));
		iView.setPadding(400, 0, 0, 0);
 		LayoutParams layoutParamsControl = new LayoutParams(
 				ViewGroup.LayoutParams.MATCH_PARENT,
 				ViewGroup.LayoutParams.MATCH_PARENT);
 		
 		((Activity)_context).getWindow().addContentView(iView, layoutParamsControl);
 		
		boardLayout.setOrientation(LinearLayout.VERTICAL);
		boardLayout.setPadding(BOARD_PADDING, BOARD_PADDING, BOARD_PADDING, BOARD_PADDING);
		
		for (int row = 0; row < BoardConstants.BOARD_TILES_NUM; row++)
		{
			LineView v = new LineView(_context, row);
			boardLayout.addView(v);
		}
		
		return boardLayout;
	}
	
	private View buildFacebookView()
	{
		ImageView imageView  = new ImageView(LayoutInflater.from(_context).getContext());
		imageView.setImageDrawable(getResources().getDrawable(R.drawable.facebook_logo));
		imageView.setPadding(32, 0, 0, 0);
		imageView.setLayoutParams(new LinearLayout.LayoutParams(96, 64));
		
		imageView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				_eventHandler.onFacebookClicked();
			}
		});
 		
 		return imageView;
	}
	
	/* Sets the given (Row x Column) position to the given character,
	 * for non-solution related positions.
	 */
	public void setLayoutLetter(char c, int row, int col)
	{
		TileView v = _tiles[row][col];
		
		v.setTextColor(Color.WHITE);
		v.setBackgroundColor(getResources().getColor(R.color.deep_green));		
		v.setText(String.valueOf(c));
	}
	
	/* Sets the given (Row x Column) position to the given character,
	 * for solution related positions.
	 */
	public void setSolutionLetter(char c, int row, int col)
	{
		TileView v = _tiles[row][col];
		
		v.setTextColor(Color.BLACK);
		v.setBackgroundColor(Color.YELLOW);
		v.setText(String.valueOf(c));
	}
	
	@Override
	public void setDialog(Dialog dialog)
	{
		// Do nothing
	}
	
	/* Simple line view, for the board view.
	 */
	private class LineView extends LinearLayout
	{
		public LineView(Context context, int row)
		{
			super(context);
			
			setOrientation(LinearLayout.HORIZONTAL);
			
			for (int col = 0; col < BoardConstants.BOARD_TILES_NUM; col++)
			{
				TileView v = new TileView(_context);
				
				v.setTextSize(15);
				v.setHeight(29);
				v.setWidth(29);
				v.setGravity(Gravity.CENTER);
				
				addView(v);
				
				_tiles[row][col] = v;
			}
		}
	}
	
	/* A single tile view, for the board view.
	 */
	private static class TileView extends TextView 
	{
		private final Paint _paint;
		private final Rect  _rect;
		
	    public TileView(Context context) 
	    {
	        super(context);
	        
	        _paint = new Paint();
	        _rect  = new Rect();
	    }

	    @Override
	    protected void onDraw(Canvas canvas) 
	    {
	        super.onDraw(canvas);

	        _paint.setStyle(Paint.Style.STROKE);
	        _paint.setColor(Color.BLACK);
	        _paint.setStrokeWidth(3);
	        
	        getLocalVisibleRect(_rect);
	        
	        canvas.drawRect(_rect, _paint);       
	    }
	}
	
	public static interface EventHandler
	{
		public void onFacebookClicked();
	}
}
